﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;
using System.Drawing;

public partial class Admin_LoaiSanPham : System.Web.UI.Page
{
    EDiamondEntities1 db = new EDiamondEntities1();
    void LoadLoaiSanPham()
    {
        gvLoaiSanPham.DataKeyNames = new string[] { "MaLoai" };
        var query = db.LoaiSanPhams;
        gvLoaiSanPham.DataSource = query;
        gvLoaiSanPham.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadLoaiSanPham();
        }
    }
    protected void gvLoaiSanPham_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvLoaiSanPham.EditIndex = -1;
        LoadLoaiSanPham();
    }
    protected void gvLoaiSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int khoa = Int32.Parse(gvLoaiSanPham.DataKeys[e.RowIndex].Value.ToString());
        try
        {
        
            LoaiSanPham entity = db.LoaiSanPhams.Include("NhaSanXuats").SingleOrDefault(p => p.MaNhaSanXuat == khoa);

            db.LoaiSanPhams.DeleteObject(entity);
            db.SaveChanges();
            lbThongBao.Text = "Hủy thành công";
            LoadLoaiSanPham();
        }
        catch (Exception ex)
        {
            lbThongBao.Text = "Hủy không thành công";
        }
    }
    protected void gvLoaiSanPham_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvLoaiSanPham.EditIndex = e.NewEditIndex;
        LoadLoaiSanPham();
    }
    protected void gvLoaiSanPham_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ten = ((TextBox)gvLoaiSanPham.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string ma = ((TextBox)gvLoaiSanPham.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        if (!string.IsNullOrEmpty(ten))
        {
            try
            {
                Int16 khoa = System.Convert.ToInt16(gvLoaiSanPham.DataKeys[e.RowIndex].Value);
                LoaiSanPham entiry = db.LoaiSanPhams.FirstOrDefault(p => p.MaNhaSanXuat == khoa);
                entiry.TenLoai = ten;
                entiry.MaNhaSanXuat = Int32.Parse(ma);
                entiry.BiDanh = Thuvien.BoDauTiengViet(ten);

                db.SaveChanges();

                gvLoaiSanPham.EditIndex = -1;
                lbThongBao.Text = "Cập nhật thành công!";
                LoadLoaiSanPham();
            }
            catch (Exception ex)
            {
                lbThongBao.Text = "Cập nhật không thành công!" + ex.Message;
            }
        }
        else
            e.Cancel = true;
    }
    protected void gvLoaiSanPham_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
        {
            LinkButton Imbt = (LinkButton)e.Row.Cells[4].Controls[2];
            Imbt.OnClientClick = "if (!confirm('Bạn có đồng ý hủy không?')) return false;";
        }
    }
}